package hu.tortavar2.server;

import hu.tortavar2.shared.Product;
import hu.tortavar2.shared.ProductCategory;
import hu.tortavar2.shared.UploadedImage;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PropertyContainer;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.ImagesServicePb.Transform;

@SuppressWarnings("serial")
public class UploadServlet extends HttpServlet {
	
	private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	private static final Logger log = Logger.getLogger(UploadServlet.class.getName());
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		ImagesService imageService = ImagesServiceFactory.getImagesService();
		Map blobs = blobstoreService.getUploadedBlobs(request);
		BlobKey blobKey = (BlobKey) blobs.get("uploadImage");
		BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
		
		Entity uploadedImage = new Entity(UploadedImage.ENTITY_NAME);
		Entity product = new Entity(Product.PRODUCT_ENTITY_NAME);
		
		String imageUrl = "";
		
		if ( blobInfo.getFilename().length() > 0 && blobKey != null) {
			imageUrl = imageService.getServingUrl(blobKey);
			uploadedImage.setProperty("blobKey", blobKey);
			uploadedImage.setUnindexedProperty(UploadedImage.SERVING_URL, imageUrl);
			uploadedImage.setProperty(Product.PRODUCT_NAME, request.getParameter("productName"));
			uploadedImage.setProperty(Product.PRODUCT_CATEGORY, "productCategory");
			datastore.put(uploadedImage);
		} else {
			blobstoreService.delete(blobKey);
			imageUrl = "http://lh5.ggpht.com/j7MZfNU8Zj5WVc4SNY6piTHtrl4yzSwzLvFnriWCoeESeGaw6R1ff4sbvM-Q4pu3hNI0Js4pTtxtcNJcRfr_Kl7H";
		}
		
		product.setProperty(Product.PRODUCT_NAME, request.getParameter("productName"));
		product.setProperty(Product.PRODUCT_PRICE, request.getParameter("productPrice"));
		product.setProperty(Product.PRODUCT_WHERE_TO_BUY, request.getParameter("whereToBuy"));
		product.setUnindexedProperty(Product.PRODUCT_IMAGE_URL, imageUrl);
		product.setProperty(Product.PRODUCT_DESCRIPTION, request.getParameter("productDescription"));
		product.setProperty(Product.PRODUCT_DESC_2, request.getParameter("productDesc2"));
		product.setProperty(Product.PRODUCT_DESC_3, request.getParameter("productDesc3"));
		product.setProperty(Product.PRODUCT_DESC_4, request.getParameter("productDesc4"));
		product.setProperty(Product.PRODUCT_DESC_5, request.getParameter("productDesc5"));
		
		product.setProperty(Product.PRODUCT_DESC_SPEC, request.getParameter("special"));
		product.setProperty(Product.PRODUCT_CATEGORY, request.getParameter("productCategory"));
		product.setProperty(Product.PRODUCT_ISACTIVE, request.getParameter("isProductActive"));
		

		//Jetty-hez
//		try {
//			ServletFileUpload upload = new ServletFileUpload();
//			response.setContentType("text/plain");
//			
//			FileItemIterator iterator = upload.getItemIterator(request);
//			
//			while (iterator.hasNext()) {
//				FileItemStream item = iterator.next();
//				InputStream stream = item.openStream();
//				
//				if(item.isFormField()) {
//						product.setProperty(item.getFieldName(), Streams.asString(stream));
//						//log.info("Got a form field: " + item.getFieldName() + "Value: " + Streams.asString(stream));
//						product.setProperty("productName", request.getParameter("productName"));
//						
//				} 
//			}
//			
//			datastore.put(product);
//			
//		} catch (FileUploadException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		datastore.put(product);
	}
	
	
// Nagyon cigány, de így adtam hozzá a két boltot...
//	Entity entity = new Entity("shops");
//	entity.setProperty("shopName", "Tortavár Buda");
//	entity.setProperty("shopAddress", "1024 Budapest Keleti Károly utca 25");
//	entity.setProperty("shopPhoneNumber", "+36 1 257-5700");
//	
//	DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
//	ds.put(entity);

//	 //Nagyon cigány, de így adtam hozzá a kategóriát is...
//		Entity entity = new Entity("ProductCategory");
//		entity.setProperty(ProductCategory.PRODUCT_CATEGORY_NAME, "Egész torta");
//		
//	
//		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
//		ds.put(entity);

//End	
}
